import { sqlMain } from '@utils/dbconf'
import { elsemap } from '@maps/index'
import { success,error,SQL_ERROR } from '@utils/response'
import axios from 'axios'
import fs from 'fs'
const ADVER_PATH = 'files/adver/'
//拉取广告
export const getadver = async (req,res) =>{
  try{
    const adver = await sqlMain(elsemap.getadver,[])
    adver.forEach(item => {
      const { filename,type } = item
      item.url = 
      `data:${type};base64,${Buffer.from(fs.readFileSync(ADVER_PATH+filename), 'binary').toString('base64')}`
      delete item.filename
    })
    return res.send(success({adver}))
  }catch(e){
    return res.send(SQL_ERROR)}
}

export const  getimg = async (req,res) =>{
try{
  const { data:{code,imgurl} } = await axios.get('https://api.ixiaowai.cn/api/api.php?return=json')
  console.log({code,imgurl})
  return res.send(code == 200?success({imgurl}):error(500,'请求异常'))
}catch(e){
  return res.send(SQL_ERROR)}
}